package com.xingda.packing.shunt.generate;

import com.xingda.packing.domain.ShuntProductionLine;
import com.xingda.packing.enums.TaskState;
import com.xingda.packing.shunt.ticket.ShuntTicket;
import com.xingda.packing.task.ticket.AbstractTaskTicket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ShuntGenerate implements TaskGenerateService {
    @Override
    public AbstractTaskTicket generate(ShuntProductionLine productionLine,int area) {
        ShuntTicket shuntTicket = new ShuntTicket();
        if (!productionLine.checkCanAssignTaskTicket(area)) {
            return null;
        }
        if (!productionLine.isScanSignal(area)) {
            return null;
        }
        int productType = productionLine.getProductType(area);
        log.info("_______________分流区工字轮类型 :{}", productType);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        productType = productionLine.getProductType(area);
        log.info("_______________分流区工字轮类型 :{}", productType);
        if (productType == 0) {
            productionLine.createNoProductTypeEvent(area);
            return null;
        }
        shuntTicket.setState(TaskState.NEW);
        shuntTicket.setProductType(productType);
        productionLine.createShuntEvent(shuntTicket,area);
        return shuntTicket;
    }
}
